package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.pojo.User;
import com.itheima.service.IUserService;
import com.itheima.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    @Autowired
    RedisTemplate redisTemplate;


    //发送验证码
    @PostMapping("/sendMsg")
    private R<String> sendMsg(@RequestBody User user, HttpServletRequest request){

        //1.获取用户名、电话
        //String name = user.getName();
        String phone = user.getPhone();

        //2.判断是否有电话号码
        if(!phone.isEmpty()){
            //2.1生成验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("code={}",code);
            //2.2调用阿里云的api发送短信
            //SMSUtils.sendMessage("瑞吉外卖","",phone,code);

            //将验证码放到Session
            //request.getSession().setAttribute("code",code);
            redisTemplate.opsForValue().set(phone,code);
            return R.success("手机验证码发送成功");
        }
        return R.error("手机验证码发送失败");
    }

    //用户登录
    @PostMapping("/login")
    private R<User> login(@RequestBody User user, HttpServletRequest request){
        //1.先从user对象中，获取电话、验证码
        String phone = user.getPhone();
        String code = String.valueOf(user.getCode());
        String realCode = (String) redisTemplate.opsForValue().get(phone);

        //2.进行判断
        //String  realCode = (String) request.getSession().getAttribute("code");
        if (!phone.isEmpty()){
            //2.2 判断验证码是否正确
            if (code.equals(realCode)){
                //2.3 验证码正确,查数据库是否有这个号码
                LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(User::getPhone,phone);

                User u = userService.getOne(queryWrapper);
                if (u==null){
                    //2.4新增用户
                    u = new User();
                    u.setPhone(phone);
                    u.setStatus(1);
                    userService.save(u);
                }
                //将userid放到session中
                request.getSession().setAttribute("user",u.getId());

                redisTemplate.delete(phone);
                return R.success(u);
            }else {
                //验证码不正确
                return R.error("验证码不正确");
            }
        }else {
            return R.error("电话号码不能为空");
        }
    }

    //退出登录
    @PostMapping("/loginout")
    private R<String> loginOut(HttpServletRequest request){
        //清除session里的userid
        request.getSession().removeAttribute("user");

        return R.success("退出登录");
    }
}
